﻿namespace Web.Control.WWW.Course
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;

    using Lx.Web.Common.ControlsUI;    
    using System.Collections.Generic;
    using System.Reflection;

    public class Search : BaseControl
    {
        /// <summary>
        /// 当前会员号
        /// </summary>
        string memberno = "";
        /// <summary>
        /// 课件类型
        /// </summary>
        protected string CType = "";
        /// <summary>
        /// 教学语言
        /// </summary>
        protected string TL = "";
        /// <summary>
        /// 适合级别
        /// </summary>
        protected string Level = "";
        /// <summary>
        /// 最大时薪
        /// </summary>
        protected string MaxPrice = "";
        /// <summary>
        /// 最小时薪
        /// </summary>
        protected string MinPrice = "";        
        /// <summary>
        /// 是否只显示母语1为TL的会员0否、1是
        /// </summary>
        protected string IsNativeBL = "0";
        /// <summary>
        /// 是否只显示免费教课的老师0否、1是
        /// </summary>
        protected string IsFree = "0";
        /// <summary>
        /// 排序： 1：UpdateDate desc、2：StarRate desc、3：Sessions desc、4：Price asc
        /// </summary>
        protected string SortBy = "1";
        /// <summary>
        /// 列表大小
        /// </summary>
        protected Literal litCount;
        protected Repeater rptCourseList;
        /// <summary>
        /// 标签列表
        /// </summary>
        protected Repeater rptTag;
        protected Repeater rptPopular;
        protected int RecordCount = 0;
        protected int PageIndex = 1;
        protected int psize = 20;
        /// <summary>
        /// 关键字搜索(course的标题、正文、tag)
        /// </summary>
        protected string Key = "";
        /// <summary>
        /// 根据tag搜索
        /// </summary>
        protected string STag = "";
        /// <summary>
        /// Popular tags for courses
        /// </summary>
        protected List<string> TagList;
        /// <summary>
        /// related tags
        /// </summary>
        protected List<string> RTagList;

        protected string C0018 = "";
        protected string C0102 = "";
        protected string C0121 = "";
        protected string TP008 = "";
        protected string TP066 = "";
        protected override void OnLoad(EventArgs e)
        {
            C0018 = GetText("C0018");
            C0102 = GetText("C0102");
            C0121 = GetText("C0121");
            TP008 = GetText("TP008");
            TP066 = GetText("TP066");

            ArrayList arrList = null;
            memberno = HttpContext.Current.User.Identity.Name;

            if (Request.Cookies["italkiLearn"] != null)
                TL = Request.Cookies["italkiLearn"].Value == "all" ? "" : Request.Cookies["italkiLearn"].Value;
            if (!string.IsNullOrEmpty(Request["p"]))
            {
                if (Request["p"] == "L")
                {
                    PageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["p"], out PageIndex);
                }
            }
            if (!string.IsNullOrEmpty(Request["sort"]))
            {
                SortBy = Request["sort"];
            }
            if (!string.IsNullOrEmpty(Request["tag"]))
            {
                STag = Request["tag"];
            }
            else if (!string.IsNullOrEmpty(Request["key"]))
            {
                Key = Request["key"];
            }
            else
            {
                if (!string.IsNullOrEmpty(Request["ctype"]))
                {
                    CType = Request["ctype"];
                }
                if (Request["tl"] != null)
                {
                    TL = Request["tl"];
                } 
                HttpCookie llCookie = new HttpCookie("italkiLearn", TL);//保存cookie
                Response.Cookies.Add(llCookie);
                if (!string.IsNullOrEmpty(Request["level"]))
                {
                    Level = Request["level"];
                }
                if (!string.IsNullOrEmpty(Request["max"]))
                {
                    MaxPrice = Request["max"];
                }
                if (!string.IsNullOrEmpty(Request["min"]))
                {
                    MinPrice = Request["min"];
                }
                if (!string.IsNullOrEmpty(Request["free"]))
                {
                    IsFree = Request["free"];
                }
                if (!string.IsNullOrEmpty(Request["istl"]))
                {
                    IsNativeBL = Request["istl"];
                }
            }
            int.TryParse(litCount.Text, out psize);

            if (Key != "")
                arrList = BLF.WWW_User.Teach.Teach.GetFindCourse_Key(TL, Key, SortBy, PageIndex, psize);
            else if (STag != "")
            {
                arrList = BLF.WWW_User.Teach.Teach.GetFindCourse_Tag(TL, STag, SortBy, PageIndex, psize);
            }
            else
            {
                //short mat;
                //short mit;
                //short.TryParse(MaxTime, out mat);
                //short.TryParse(MinTime, out mit);
                //if (mat == 0) mat = 6;
                //else MaxTime = ((float)(mat / 2)).ToString();
                //if (mit != 0) MinTime = ((float)(mit / 2)).ToString();

                decimal maP;
                decimal miP;
                decimal.TryParse(MaxPrice, out maP);
                decimal.TryParse(MinPrice, out miP);
                if (maP == 0)
                {
                    maP = 999999;
                }
                arrList = BLF.WWW_User.Teach.Teach.GetFindCourse(TL, Level, CType, maP, miP, IsFree == "1" ? true : false, IsNativeBL == "1" ? true : false, SortBy, PageIndex, psize);
            }
            if (arrList != null)
            {
                RecordCount = (int)arrList[0];
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)psize));
                if (PageIndex > pCount) PageIndex = pCount;
                
                if (rptCourseList != null)
                {
                    rptCourseList.DataSource = arrList[1];
                    rptCourseList.DataBind();
                }
                TagList = arrList[2] as List<string>;
                if (rptPopular != null)
                {
                    List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();
                    lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                    rptPopular.DataSource = lanObjList;
                    rptPopular.DataBind();
                }
                if (STag != "")
                {
                    RTagList = arrList[3] as List<string>;
                }
                //title
                this.DynamicDescription = GetText("Find a language course at italki");
                if (!string.IsNullOrEmpty(Request["tl"]) && Request["ctype"] == null && Request["tag"] == null && Request["level"] == null)
                {
                    this.DynamicTitle = GetText("Find a Language Course - teach {0} - italki", new string[] { BLF.StaticData.GetLanguageText(TL) });
                    this.DynamicKeywords = GetText("course, language course, {0}", new string[] { BLF.StaticData.GetLanguageText(TL) });
                }
                else if (!string.IsNullOrEmpty(Request["ctype"]) && Request["tl"] == null && Request["tag"] == null && Request["level"] == null)
                {
                    this.DynamicTitle = GetText("{0} - italki Courses", new string[] { BLF.WWW_User.Teach.Teach.GetCTypeText(CType) });
                    this.DynamicKeywords = GetText("{0} , course, language course", new string[] { BLF.WWW_User.Teach.Teach.GetCTypeText(CType) });
                    this.DynamicDescription = GetText("Courses classified with {0} at italki", new string[] { BLF.WWW_User.Teach.Teach.GetCTypeText(CType) });
                }
                else if (!string.IsNullOrEmpty(Request["tag"]) && Request["tl"] == null && Request["ctype"] == null && Request["level"] == null)
                {
                    this.DynamicTitle = GetText("{0} - italki Courses", new string[] { STag });
                    this.DynamicKeywords = GetText("{0} , course, language course", new string[] { STag });
                    this.DynamicDescription = GetText("Courses tagged with {0} at italki", new string[] { STag });
                }
                else if (!string.IsNullOrEmpty(Request["level"]) && Request["tl"] == null && Request["ctype"] == null && Request["tag"] == null)
                {
                    this.DynamicTitle = GetText("Find a Language Course - {0} - italki", new string[] { BLF.WWW_User.Users.User.GetLevelText(Level) });
                    this.DynamicKeywords = GetText("course, language course, {0}", new string[] { BLF.WWW_User.Users.User.GetLevelText(Level) });
                }
                else {
                    this.DynamicTitle = GetText("Find a Language Course - italki");
                    this.DynamicKeywords = GetText("course, language course");
                }
            }
            else
            {
                Response.End();
                return;
            }
            base.OnLoad(e);
        }
        /// <summary>
        /// 获取页面排序URL连接
        /// </summary>
        /// <returns></returns>
        protected string GetSortUrlText()
        {
            string strUrl = "";
            string sort1 = GetText("TP134");
            string sort2 = GetText("TP131");
            string sort3 = GetText("TP132");
            string sort4 = GetText("TP133");
            switch (SortBy)
            {
                case "1":
                    if (Key != "")
                        strUrl = string.Format("<li><strong>{0}</strong></li><li><a href='{4}/course/search.htm?sort=2&key={5}'>{1}</a></li><li><a href='{4}/course/search.htm?sort=3&key={5}'>{2}</a></li><li class='last'><a href='{4}/course/search.htm?sort=4&key={5}'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, Key);
                    else if (STag == "")
                        strUrl = string.Format("<li><strong>{0}</strong></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-2.htm'>{1}</a></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-3.htm'>{2}</a></li><li class='last'><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-4.htm'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, TL, Level, CType, MinPrice, MaxPrice, IsFree, IsNativeBL);
                    else
                        strUrl = string.Format("<li><strong>{0}</strong></li><li><a href='{4}/course/tag/{5}.htm?sort=2'>{1}</a></li><li><a href='{4}/course/tag/{5}.htm?sort=3'>{2}</a></li><li class='last'><a href='{4}/course/tag/{5}.htm?sort=4'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, STag);
                    break;
                case "2":
                    if (Key != "")
                        strUrl = string.Format("<li><a href='{4}/course/search.htm?sort=1&key={5}'>{0}</a></li><li><strong>{1}</strong></li><li><a href='{4}/course/search.htm?sort=3&key={5}'>{2}</a></li><li class='last'><a href='{4}/course/search.htm?sort=4&key={5}'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, Key);
                    else if (STag == "")
                        strUrl = string.Format("<li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-1.htm'>{0}</a></li><li><strong>{1}</strong></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-3.htm'>{2}</a></li><li class='last'><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-4.htm'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, TL, Level, CType, MinPrice, MaxPrice, IsFree, IsNativeBL);
                    else
                        strUrl = string.Format("<li><a href='{4}/course/tag/{5}.htm?sort=1'>{0}</a></li><li><strong>{1}</strong></li><li><a href='{4}/course/tag/{5}.htm?sort=3'>{2}</a></li><li class='last'><a href='{4}/course/tag/{5}.htm?sort=4'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, STag);
                    break;
                case "3":
                    if (Key != "")
                        strUrl = string.Format("<li><a href='{4}/course/search.htm?sort=1&key={5}'>{0}</a></li><li><a href='{4}/course/search.htm?sort=2&key={5}'>{1}</a></li><li><strong>{2}</strong></li><li class='last'><a href='{4}/course/search.htm?sort=4&key={5}'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, Key);
                    else if (STag == "")
                        strUrl = string.Format("<li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-1.htm'>{0}</a></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-2.htm'>{1}</a></li><li><strong>{2}</strong></li><li class='last'><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-4.htm'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, TL, Level, CType, MinPrice, MaxPrice, IsFree, IsNativeBL);
                    else
                        strUrl = string.Format("<li><a href='{4}/course/tag/{5}.htm?sort=1'>{0}</a></li><li><a href='{4}/course/tag/{5}.htm?sort=2'>{1}</a></li><li><strong>{2}</strong></li><li class='last'><a href='{4}/course/tag/{5}.htm?sort=4'>{3}</a></li>", sort1, sort2, sort3, sort4, WWWHost, STag);
                    break;
                case "4":
                    if (Key != "")
                        strUrl = string.Format("<li><a href='{4}/course/search.htm?sort=1&key={5}'>{0}</a></li><li><a href='{4}/course/search.htm?sort=2&key={5}'>{1}</a></li><li><a href='{4}/course/search.htm?sort=3&key={5}'>{2}</a></li><li class='last'><strong>{3}</strong></li>", sort1, sort2, sort3, sort4, WWWHost, Key);
                    else if (STag == "")
                        strUrl = string.Format("<li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-1.htm'>{0}</a></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-2.htm'>{1}</a></li><li><a href='{4}/course/1-{5}-{6}-{7}-{8}-{9}-{10}-{11}-3.htm'>{2}</a></li><li class='last'><strong>{3}</strong></li>", sort1, sort2, sort3, sort4, WWWHost, TL, Level, CType, MinPrice, MaxPrice, IsFree, IsNativeBL);
                    else
                        strUrl = string.Format("<li><a href='{4}/course/tag/{5}.htm?sort=1'>{0}</a></li><li><a href='{4}/course/tag/{5}.htm?sort=2'>{1}</a></li><li><a href='{4}/course/tag/{5}.htm?sort=3'>{2}</a></li><li class='last'><strong>{3}</strong></li>", sort1, sort2, sort3, sort4, WWWHost, STag);
                    break;
            }
            return strUrl;
        }
    }
}
